var grid01 = $('#jgTable01'),
    grid02 = $('#jgTable02'),
    grid0m = $('#jgTable0m');

var Father = '', Father_Name = '', _Data = [], checked = [], mData = [];

var postData02 = {
    Method: 'Q',
    Content:{
        Code: Father,
        SearchText: ''
    }
};
for(i in postData){
    postData02[i] = postData[i];
}

$(function(){

    gridInit01();
    gridInit02();
    gridInit0m();
    $('li[data-opt="add"]').on('click', add);
    $('#BrandBtn').on('click', update);
    $('.modal-data').draggable();

});

function gridInit01(){  //MDM0025/MDM002502
    var gridData01 = Object.create(gridData);
    gridData01.grid = $('#jgTable01');
    gridData01.pager = $('#jgPager01');
    gridData01.scroll = true;
    gridData01.height = '390px';
    gridData01.rownumbers = false;
    gridData01.url = mdmPath + '/MDM0025/MDM002502';
    gridData01.colModel = [
        {label:'代码', name:'Code', sortable:false, hidden:true},
        {label:'名称', name:'Name', sortable:false}
    ];
    gridData01.rowNum = 20;
    gridData01.sortname = 'Code';
    var postData01 = {
        Method: 'Q',
        Content:{
            SearchText: ''
        }
    };
    for(i in postData){
        postData01[i] = postData[i];
    }
    //console.log(postData)
    gridData01.beforeRequest = function(){
        gridData01.grid.jqGrid("setGridParam", {
            postData : postData01,
        });
    };
    gridData01.loadComplete = function(xhr){
        xhr.Status == -999 && alert_QX($('body'), xhr.Message);
        setTimeout(clickTr,200);
    };
    gridData01.onSelectRow = function(rowid,status){
        var rowData = grid01.jqGrid('getRowData', rowid);  
        //console.log(rowData)
        Father = rowData.Code;
        Father_Name = rowData.Name;
        postData02 = {
            Content: {
                Code: Father,
                SearchText: ''
            }
        }
        grid02.trigger('reloadGrid');
    };
    gridInit(gridData01);
}
function alarmFormatter(cellvalue, options, rowdata){
    var grid = $(this);
    if(JSON.stringify(rowdata)!=='{}'){
        cellvalue = '<a href="javascript:0" opt-data="'+grid[0].id+'_delete" title="删除"><i class="fa fa-trash-o"></i></a>';
    }else{
        cellvalue = '';
    }
    return cellvalue;
}
function gridInit02(){  //MDM0025/MDM002503
    var gridData02 = Object.create(gridData);
    gridData02.grid = $('#jgTable02');
    //gridData02.pager = $('#jgPager02');
    gridData02.url = mdmPath + '/MDM0025/MDM002503';
    gridData02.colModel = [
        {label:'员工代码', name:'EmployeeCode'},
        {label:'员工名称', name:'EmployeeName'},
        {label:'操作', name:'Options', width:80, formatter:alarmFormatter, sortable:false}
    ];
    gridData02.rowNum = 200;
    gridData02.beforeRequest = function(){
        gridData02.grid.jqGrid("setGridParam", {
            postData : postData02,
        }); 
    };
    gridData02.loadComplete = function(xhr){
        xhr.Status == -999 && alert_situ($('.u_row').find('.u_block_content'),re.Message);
        $('a[opt-data="jgTable02_delete"]').unbind('click').bind('click', function(){
            var rowId = $(this).parent().parent()[0].id;
            grid02.jqGrid("delRowData", rowId);
            console.log(grid02.jqGrid("getRowData"));
            var _postData = {
            Method: 'U',
                Content: [{
                    Code: Father,
                    Name: Father_Name,
                    MDM002502: grid02.jqGrid("getRowData")
                }]
            }
            for(i in postData){
                _postData[i] = postData[i]
            }
            $.ajax({
                type: 'post',
                url: mdmPath + "/MDM0025/MDM002501",
                data: _postData,
                dataType: "json",
                success: function (re) {
                    if(re.Status == 200){
                        grid02.trigger('reloadGrid');
                        myAlert('删除成功');
                        $('.modal-overlay').addClass('hidden');
                    }else{
                        myAlert(re.Message);
                    }
                }
            })
        });
    }
    gridData02.jsonReader = {
        root: "Content.MDM002502",   //数据入口
        total: "Content.total",
        records: "Content.records"
    };
    gridInit(gridData02);
}
function gridInit0m(){
    var gridData0m = Object.create(gridData);
    gridData0m.grid = $('#jgTable0m');
    gridData0m.pager = $('#jgPager0m');
    gridData0m.url = mdmPath + '/MDM0001/MDM000102';
    gridData0m.colModel = [
        {label:'员工代码', name:'Code'},
        {label:'员工名称', name:'Name'}
    ];
    gridData0m.sortname = 'Code';
    var postData0m = {
        Method: 'Q',
        Content:{
            SearchText: ''
        }
    };
    for(i in postData){
        postData0m[i] = postData[i];
    }
    gridData0m.beforeRequest = function(){
        gridData0m.grid.jqGrid("setGridParam", {
            postData: postData0m,
        });
    };
    gridData0m.loadComplete = function(xhr){
        $("li[data-opt='opt_search']").find(">i").unbind('click').bind('click', function () {
            postData.Content.SearchText = $("#opt_search").find(">input").val();
            gridData0m.grid.trigger("reloadGrid");
        });
        var rowData = grid0m.jqGrid('getRowData');
        mData = mData.concat(rowData).unique('Code');
        rowData.forEach(function(v,i,a){
            checked.indexOf(v.Code)!=-1 
            && grid0m.jqGrid('setSelection',i+1);
        });
    };
    gridData0m.onSelectRow = function(rowid,status){
        //console.log(rowid, status)
        var rowData = grid0m.jqGrid('getRowData', rowid);
        var rowNo = rowData.Code;
        if(status){  
            checked.indexOf(rowNo)==-1 
            && checked.push(rowNo);
        }else{
            checked.splice(checked.indexOf(rowNo),1);
        }
        //console.log(checked)
    };
    gridData0m.multiselect = true;
    gridData0m.onSelectAll = function(rowids,status){
        var rowData = grid0m.jqGrid('getRowData');
        if(status){
            rowData.forEach(function(v,i,a){
                checked.indexOf(v.Code)==-1
                && checked.push(v.Code);
            });
        }else{
            rowData.forEach(function(v,i,a){
                checked.indexOf(v.Code)!=-1
                && checked.splice(checked.indexOf(v.Code),1);
            });
        }
        //console.log(checked);
    };
    gridInit(gridData0m);
    gridSearch = postData0m;
}

//Options
function add(){
    $('#modal1').removeClass('hidden');
    $('#submit1').unbind('click').bind('click', function(){
        var _postData = {
            Method: 'A',
            Content: [{
                Code: $('#m_Code').val(),
                Name: $('#m_Name').val()
            }]
        }
        for(i in postData){
            _postData[i] = postData[i]
        }
        $.ajax({
            type: 'post',
            url: mdmPath + "/MDM0025/MDM002501",
            data: _postData,
            dataType: "json",
            success: function (re) {
                if(re.Status == 200){
                    grid01.trigger('reloadGrid');
                    myAlert('添加成功');
                    $('.modal-overlay').addClass('hidden');
                }else{
                    myAlert(re.Message);
                }
            }
        })
    });
}
function update(){
    $('#modal2').removeClass('hidden');    
    gridSize();
    $('#submit2').unbind('click').bind('click', function(){
        var MDM002502 = grid02.jqGrid('getRowData');
        mData.forEach(function(v,i,a){
            checked.indexOf(v.Code)!=-1 && MDM002502.push({EmployeeCode:v.Code, EmployeeName:v.Name});
        });
        var _postData = {
            Method: 'U',
            Content: [{
                Code: Father,
                Name: Father_Name,
                MDM002502: MDM002502
            }]
        }
        for(i in postData){
            _postData[i] = postData[i]
        }
        $.ajax({
            type: 'post',
            url: mdmPath + "/MDM0025/MDM002501",
            data: _postData,
            dataType: "json",
            success: function (re) {
                if(re.Status == 200){
                    grid02.trigger('reloadGrid');
                    myAlert('添加成功');
                    $('.modal-overlay').addClass('hidden');
                }else{
                    myAlert(re.Message);
                }
            }
        })
    });
}

//Style
function clickTr() {
    $('#jgTable01').find('>tbody>tr:nth-child(2)').trigger('click');
}
function gridSize() {
    grid0m.setGridWidth($('.u_modal_body').eq(1).width());
    grid02.setGridWidth($('.u_row .u_table_container').width());
}
